Skip to content

Fix GemmTransposeFusion handling of identity Transpose - #32428

Closed
Sylvester Kaczmarek (sylvesterkaczmarek) wants to merge 4 commits into
microsoft:mainfrom
sylvesterkaczmarek:fix/gemm-transpose-identity-perm
Closed

Fix GemmTransposeFusion handling of identity Transpose#32428
Sylvester Kaczmarek (sylvesterkaczmarek) wants to merge 4 commits into
microsoft:mainfrom
sylvesterkaczmarek:fix/gemm-transpose-identity-perm

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown
Contributor

Description

Fixes #32228.

GemmTransposeFusion currently treats any Transpose adjacent to Gemm as a matrix transpose and toggles transA or transB without checking the perm attribute. For an identity transpose (perm=[0,1]), removing the node and toggling the Gemm flag changes the computation and can silently produce incorrect results.

This change only folds Transpose nodes that actually swap the two Gemm matrix axes ([1,0], including the rank-2 default permutation). The check is applied to input A, input B, and output Transpose paths.

Tests

Adds regression coverage showing that perm=[0,1] is left intact with transA unchanged, while perm=[1,0] continues to fuse.

Copilot AI balanced review requested due to automatic review settings September 3, 2026 18:33
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The newly guarded input-B and output-Transpose paths need regression coverage.

Pull request overview

Fixes incorrect GemmTransposeFusion handling of identity transposes.

Changes:

  • Restricts fusion to matrix transposes using [1, 0] or the rank-2 default permutation.
  • Adds input-A regression coverage.
File summaries
File Review
onnxruntime/test/optimizer/gemm_transpose_fusion_test.cc Tests input-A permutations; add identity-permutation coverage for input B and output Transpose paths.
onnxruntime/core/optimizer/gemm_transpose_fusion.cc Validates permutations across all Gemm paths; add identity and [1, 0] tests for input B and output Transpose behavior.
Review details

Suppressed comments (3)

onnxruntime/core/optimizer/gemm_transpose_fusion.cc:63

  • The regression tests only place the Transpose on input A, so the newly guarded input-B path is not exercised. A future omission or regression of this IsMatrixTranspose check would still pass both new tests; add corresponding identity and [1, 0] cases with the Transpose feeding Gemm input 1 and assert transB.
  if (B_node_ptr != nullptr && B_node_ptr->OpType() == "Transpose" && IsMatrixTranspose(*B_node_ptr)) {

onnxruntime/core/optimizer/gemm_transpose_fusion.cc:84

  • The output-Transpose permutation check is also new behavior, but the added tests cover only an input Transpose. Add output-side identity and [1, 0] cases so this path verifies that identity remains and a real matrix transpose still fuses with the expected swapped inputs/flags.
      output_node_ptr->OpType() == "Transpose" &&
      IsMatrixTranspose(*output_node_ptr)) {

onnxruntime/test/optimizer/gemm_transpose_fusion_test.cc:72

  • These regressions exercise the new permutation check only for input A. The same predicate now gates input B and the output-Transpose rewrite, but neither path verifies that perm={0, 1} is preserved; a future omission or branch-specific regression there would still pass. Please add identity-permutation cases for B and the output path (the existing graph-transform tests cover their successful matrix-transpose fusion).
TEST(GemmTransposeFusionTest, IdentityInputTransposeIsNotFolded) {
  RunInputTransposeTest({0, 1}, {2, 3}, false, 0);
}
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@xadupre

Copy link
Copy Markdown
Member

duplicate of #32429

@sylvesterkaczmarek

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate of #32429.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] GemmTransposeFusion folds an identity Transpose (perm=[0,1]) into Gemm transA/transB, producing silently wrong results

3 participants